package d3_method_reference;

import java.util.Arrays;
import java.util.Comparator;

public class Test02 {
    public static void main(String[] args) {
        //目标: 理解实例方法引用
        //目标: 理解静态方法引用
        Student[] students = new Student[4];
        //进行数据封装
        students[0] = new Student("周芷若", 21, '女', 169.3);
        students[1] = new Student("殷素素", 38, '女', 172.2);
        students[2] = new Student("小昭", 19, '女', 168.5);
        students[3] = new Student("张无忌", 23, '男', 183.5);


        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return Double.compare(o1.getHeight(), o2.getHeight());
            }
        });

        Arrays.sort(students, (o1, o2) -> {
            return Double.compare(o1.getHeight(), o2.getHeight());
        });
        //为什么要搞出Lambda表达式,因为代码过于臃肿-->不是比较好的语言,所以简化一下
        Arrays.sort(students, (o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()));//sort底层会调用重写之后的方法

        //实例方法引用
        //也要特定场景
        //规则:如果某个Lambda表达式只是调用一个实力方法,并且前后参数形式一致,就可以使用实例实例方法引用
        Test02 t = new Test02();
        Arrays.sort(students, (o1, o2) -> t.compare(o1, o2));
        Arrays.sort(students,  t::compare);//终级简化代码

    }

    //利用实例方法对重写的代码进行封装
    public int compare(Student o1, Student o2){
        return Double.compare(o1.getHeight(), o2.getHeight());
    }
}
